package com.nwpu.server.admin.controller;

import com.nwpu.server.admin.common.Constant;
import com.nwpu.server.admin.entity.NetworkEntity;
import com.nwpu.server.admin.service.NetworkService;
import com.nwpu.server.common.exception.BizCodeEnum;
import com.nwpu.server.common.response.PageUtils;
import com.nwpu.server.common.response.R;
import com.nwpu.server.common.util.FormatUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * @author YuXingZh
 * @email 617058979@qq.com
 * @date 2022-02-19 16:15:43
 */
@Slf4j
@RestController
@RequestMapping("admin/network")
public class NetworkController {
    @Autowired
    private NetworkService networkService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = networkService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/list1")
    public R list1(@RequestParam Map<String, Object> params) {
        params.put("key", "edgelist");

        PageUtils page = networkService.queryPage1(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/list2")
    public R list2(@RequestParam Map<String, Object> params) {
        params.put("key", "emb");

        PageUtils page = networkService.queryPage1(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/list3")
    public R list3(@RequestParam Map<String, Object> params) {
        params.put("key", "txt");

        PageUtils page = networkService.queryPage1(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        NetworkEntity network = networkService.getById(id);

        return R.ok().put("network", network);
    }

    /**
     * 上传文件
     */
    @RequestMapping(value = "/uploadFiles", method = {RequestMethod.POST})
    @ResponseBody
    public R uploadFiles(@RequestParam("file") MultipartFile file) {
        log.info("开始咯");
        if (file == null && file.isEmpty()) {
            return R.error(BizCodeEnum.FILE_IS_NULL.getCode(), BizCodeEnum.FILE_IS_NULL.getMsg());
        }

        String fileName = FormatUtils.getUUID();
        Date current = new Date();
        String fileOriginalFilename = file.getOriginalFilename();
        String fileType = fileOriginalFilename.substring(fileOriginalFilename.lastIndexOf(".") + 1);
        log.info(fileType);
        String dir;

        if ("edgelist".equals(fileType)) {
            dir = Constant.AddressType.NETWORK.getValue();
        } else if ("emb".equals(fileType)) {
            dir = Constant.AddressType.FEATURE.getValue();
        } else if ("txt".equals(fileType)) {
            dir = Constant.AddressType.DISEASE.getValue();
        } else {
            return R.error(BizCodeEnum.DIR_UPLOAD_EXCEPTION.getCode(), BizCodeEnum.DIR_UPLOAD_EXCEPTION.getMsg());
        }

        log.info("上传呀");
        // 根据文件的全路径名字(含路径、后缀),new一个File对象dest
        File dest = new File(dir + "/" + fileName + "." + fileType);

        try {
            // 将获取到的附件file,transferTo写入到指定的位置(即:创建dest时，指定的路径)
            file.transferTo(dest);
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        NetworkEntity networkEntity = new NetworkEntity();
        networkEntity.setId(fileName);
        networkEntity.setFileName(file.getOriginalFilename());
        networkEntity.setFileSize(String.valueOf(file.getSize()));
        networkEntity.setFileType(fileType);
        networkEntity.setFileDir(dir);
        networkEntity.setCreateTime(current);
        networkEntity.setUpdateTime(current);
        networkEntity.setShowStatus(1);
        networkService.save(networkEntity);

        log.info("保存咯");
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody NetworkEntity network) {
        networkService.updateById(network);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody String[] ids) {
        networkService.removeNetworkByIds(Arrays.asList(ids));

        return R.ok();
    }

}
